package com.sd.activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

import com.sd.activity.r.R;
import com.sd.reader.ReaderSet;
import com.sd.view.ReaderSetAdapter;
import com.sd.view.ReadsetDialogManager;

/**
 * 
 * 阅读设置
 * 
 * @author Alex.Lu
 * 
 */
public class ReadSetActivity extends BaseActivity implements OnClickListener, OnItemClickListener, android.content.DialogInterface.OnClickListener {

	private static int what;
	
	private ImageButton mReturnIB;
	
	private ListView mReadsetLV;
	
	private TextView mDemoTextTV;
	
	private static int mBookId;
	
	public static Intent ReadBookTo(Context context,int bookId){
		mBookId = bookId;
		what = 1;
		Intent intent = new Intent(context, ReadSetActivity.class);
		return intent;
	}
	
	public static Intent BookshelfTo(Context context){
		what = 0;
		Intent intent = new Intent(context, ReadSetActivity.class);
		return intent;
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_readerset);
		
		mDemoTextTV = (TextView)findViewById(R.id.demo_text);
		
		mReturnIB = (ImageButton)findViewById(R.id.return_btn);
		mReturnIB.setOnClickListener(this);
		
		mReadsetLV = (ListView)findViewById(R.id.readset_items);
		ReaderSetAdapter adapter = new ReaderSetAdapter(this);
		mReadsetLV.setAdapter(adapter);
		mReadsetLV.setOnItemClickListener(this);
		
		initDemoText();
	}
	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		int v_id = v.getId();
		switch (v_id) {
		case R.id.return_btn:
			backReadSet();
			break;
		case R.id.btn_ok:
			ReadsetDialogManager.getInstance().dismiss();
			mDemoTextTV.setTextSize(ReaderSet.getInstance().getmFontSize());
			mDemoTextTV.setTextColor(ReaderSet.getInstance().getFontColor());
			mDemoTextTV.setBackgroundResource(ReaderSet.getInstance().getReadBg());
			Typeface taypface = Typeface.create(ReaderSet.getInstance().getFontFamily(), ReaderSet.getInstance().getFontStyle());
			mDemoTextTV.setTypeface(taypface);
			break;
		default:
			break;
		}
	}


	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		// TODO Auto-generated method stub
		switch (position) {
		case 0:
			ReadsetDialogManager.getInstance().createFontsize(this, this);
			break;
		case 1:
			ReadsetDialogManager.getInstance().createFamily(this, this);
			break;
		case 2:
			ReadsetDialogManager.getInstance().createBG(this, this);
			break;
		case 3:
			ReadsetDialogManager.getInstance().createFontStyle(this, this);
			break;
		case 4:
			ReadsetDialogManager.getInstance().createReadModel(this, this);
			break;
		default:
			break;
		}
	}



	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}



	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		backReadSet();
		super.onBackPressed();
	}

	@Override
	public void onClick(DialogInterface dialog, int which) {
		// TODO Auto-generated method stub
		initDemoText();
		
	}
	
	private void initDemoText(){
		mDemoTextTV.setTextSize(ReaderSet.getInstance().getmFontSize());
		mDemoTextTV.setTextColor(ReaderSet.getInstance().getFontColor());
		mDemoTextTV.setBackgroundResource(ReaderSet.getInstance().getReadBg());
		Typeface taypface = Typeface.create(ReaderSet.getInstance().getFontFamily(),Typeface.NORMAL);
		mDemoTextTV.setTypeface(taypface, ReaderSet.getInstance().getFontStyle());
	}
	
	private void backReadSet(){
		finish();
		ReaderSet.getInstance().commitReaderSet();
		
		switch (what) {
		case 1:
			Intent intent = new Intent();
			intent.putExtra(ReadBookActivity.BOOK_ID, mBookId);
			intent.setClass(this, ReadBookActivity.class);
			startActivity(intent);
			break;
		case 0:
			
			break;

		default:
			break;
		}
	}
	


}